package de.fhluebeck.oop.gps.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.fhluebeck.oop.gps.common.Singleton;
import de.fhluebeck.oop.gps.common.util.PropertiesHelper;

/**
 *
 * Abstrahiert die Verbindung zur Datenbank und den Zugriff auf die DataSource
 * Intialisiert die Datenbank
 *
 * @author Ogün Bilge, Leif Hitzschke
 * @version 1.00, 13.11.2009
 * 
 */
public class Database implements Singleton {

	private final static Logger log = LoggerFactory.getLogger(Database.class);

	private DataSource dataSource;

	private Database() {
	}

	// Holder Idiom Pattern
	private static class Holder {
		private static final Database instance = new Database();
	}

	public static Database getInstance() {
		return Holder.instance;
	}

	public Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}

	public void initialize() {

		Properties props = PropertiesHelper.loadConfigProperties();
		dataSource = new BaseDataSource(props.getProperty("db.driver"), props
				.getProperty("db.url"), props.getProperty("db.username"), props
				.getProperty("db.password"));
	}

}
